package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.UsertypeEmployee;
import com.cashmate.hibernate.pojo.Usertypes;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.DetachedCriteria;

public class UserTypeDAO extends DAO {

    public UserTypeDAO() {
        super();
    }
//BASIC DAO METHODS

    @SuppressWarnings("unchecked")
    public List<Usertypes> getAll() throws Exception {
        List<Usertypes> a = (List<Usertypes>) getSession().createQuery("from Usertypes a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Usertypes get(int id) throws Exception {
        Usertypes a = (Usertypes) getSession().createQuery("from Usertypes a where a.usertypeid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Usertypes a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Usertypes a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Usertypes> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Usertypes> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }
    
    //OTHER METHODS 

    @SuppressWarnings("unchecked")
    public String validateEmployee(int eid, String usertypepass) throws Exception {
        Usertypes ut = (Usertypes) getSession().createQuery("from Usertypes u where u.password=:pass").setParameter("pass", usertypepass).setMaxResults(1).setCacheable(true).uniqueResult();
        if (ut != null) {
            for (UsertypeEmployee ute : ut.getUsertypeEmployees()) {
                if (ute.getEmployees().getEmployeeid() == eid) {
                    return ute.getUsertypes().getUsertypeid() + "";
                }
            }
        } else {
            return "none";
        }
        return "invalid";
    }

    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(String type) throws Exception {
        for (Usertypes u : getAll()) {
            if (u.getType().equals(type)) {
                return true;
            }
        }
        return false;
    }
}
